<?php
header("Content-Type: text/plain; charset=utf-8");

set_time_limit(0);

define('ROOT_PATH' , '../');

require_once('includes/database.class.php');
require_once('includes/functions.php');
require_once('../conf/base.conf.php');
$DB = new database(DB_HOST , DB_USER , DB_PASSWORD , DB_NAME);

$dirname = dirname(__FILE__) . '/';

$success_num = 0;

// 先获取最新的id
$page = get_page('http://armorgames.com/');
if(empty($page)) die('Get http://armorgames.com/ Failed.');
$start_pos = strpos($page , '<optgroup label="New Games">');
$match = array();
preg_match('%<option value="/play/([0-9]+)/%' , substr($page , $start_pos) , $match);
if(empty($match) || empty($match[1])) die('Get MAX ID Failed.');
$max_id = $match[1];

// $max_id = 15000;

// 获取上次的id
$last_id = $DB->result("SELECT `rid` FROM `spider` WHERE `type` = 'armorgames' ORDER BY `id` DESC");
$last_id = empty($last_id) ? 10 : $last_id;

if($max_id <= $last_id) die('max_id <= last_id , no new.');

for($id = $last_id + 1 ; $id <= $max_id ; $id++) {
	$url = 'http://armorgames.com/play/' . $id. '/age-of-defense';
	echo $url . "\n";
	
	$page = get_page($url);
	if(empty($page)) {
		// retry
		$page = get_page($url);
	}
	
	if(empty($page)) {
		echo "No data.\n\n";
		continue;
	}
	
	$title = getTitle($page);
	$thumb = getThumb($page);
	$flash = getFlash($page);
	$desc  = getDescription($page);
	$control = getControl($page);
	$category = getCategory($page);
	
	$control = str_replace(array("<br />\n" , "<br />") , array("\n" , "\n") , $control);
	$desc = str_replace(array("<br />\n" , "<br />") , array("\n" , "\n") , $desc);
	
	$insert_array = array(
		'name' => $title,
		'desc' => $desc,
		'control' => $control,
	);
	
	
	// 抓取flash
	@unlink('temp.swf');
	system("wget -q -O temp.swf '{$flash}'");
	// 判断大小
	if(!file_exists('temp.swf') || filesize('temp.swf') < (25*1024)) {
		echo "{$flash} is too small (' " . filesize('temp.swf') . " ').\n\n";
		continue;
	}
	
	// 抓取图片
	@unlink('temp.jpg');
	system("wget -q -O temp.jpg '{$thumb}'");
	// 判断大小
	if(!file_exists('temp.jpg')) {
		echo "{$thumb} get failed.\n\n";
		continue;
	}
	
	// 写入数据库
	$slug = getSlug($title);
	$insert_array['slug'] = $slug;
	$insert_array['time'] = time();
	$insert_array['swf_size'] = filesize('temp.swf');
	
	$success = $DB->doInsert('items' , $insert_array);
	$item_id = $DB->insertId();
	
	// 更新tag
	$tagslug = getSlug($category);
	$tagname = ucwords($tagslug);
	$tagid = $DB->result("SELECT `tagid` FROM `tags` WHERE `tagslug` = '{$tagslug}'");
	if(empty($tagid)) {
		$DB->doInsert('tags' , array(
			'tagslug' => $tagslug,
			'tagname' => $tagname,
		));
		$tagid = $DB->insertId();
	} else {
		
	}
	$DB->doInsert('tag_item' , array(
		'tagid' => $tagid,
		'itemid' => $item_id,
	));
	
	
	// 放swf和图片
	$dir_index = floor($item_id / 1000);
	$flash_dir = ROOT_PATH . '/media/flash/' . $dir_index . '/';
	$thumb_dir = ROOT_PATH . '/media/thumb/' . $dir_index . '/';
	if(!file_exists($flash_dir)) {
		mkdir($flash_dir , 0777);
	}
	rename('temp.swf' , $flash_dir . $slug . '_' . $item_id . '.swf');
	
	if(!file_exists($thumb_dir)) {
		mkdir($thumb_dir , 0777);
	}
	rename('temp.jpg' , $thumb_dir . $slug . '_' . $item_id . '.jpg');
	
	
	// 记录到 spider 表
	$insert_array = array(
		'id' => $item_id,
		'rid' => $id,
		'type' => 'armorgames',
		'time' => time(),
	);
	$DB->doInsert('spider' , $insert_array);
	
	echo "ok\n\n";
	
	$success_num++;
	if($success_num >= 10) break;
}

function getSlug($name) {
	$name = preg_replace('%[^a-z0-9_]%i' , '_' , $name);
	$name = preg_replace('%_{2,}%' , '_' , $name);
	$name = preg_replace('%_$%' , '' , $name);
	$name = preg_replace('%^_%' , '' , $name);
	$name = strtolower($name);
	return $name;
}

function getTitle($page) {
	$match = array();
	preg_match('%<title>(.+?) \| Armor Games</title>%' , $page , $match);
	return $match[1];
}

function getThumb($page) {
	$match = array();
	preg_match('%"(http://cache.armorgames.com/files/thumbnails/.+?)"%' , $page , $match);
	return $match[1];
}

function getFlash($page) {
	$match = array();
	preg_match('%(/files/games/.+?\.swf)"%' , $page , $match);
	if(empty($match)) return false;
	return 'http://armorgames.com' . $match[1];
}

function getDescription($page) {
	$start_pos = strpos($page , '<div id="description">');
	$page = substr($page , $start_pos);
	$start_pos = strpos($page , '<p>');
	$page = substr($page , $start_pos + 3);
	$end_pos = strpos($page , '</p>');
	$page = substr($page , 0 , $end_pos);
	return $page;
}

function getControl($page) {
	$start_pos = strpos($page , '<div id="controls">');
	$page = substr($page , $start_pos);
	$start_pos = strpos($page , '<p>');
	$page = substr($page , $start_pos + 3);
	$end_pos = strpos($page , '</p>');
	$page = substr($page , 0 , $end_pos);
	return $page;
}

function getCategory($page) {
	$start_pos = strpos($page , '<p class="breadcrumb">');
	$page = substr($page , $start_pos);
	$match = array();
	preg_match('%/category/(.+?)"%' , $page , $match);
	if(empty($match)) return false;
	
	return $match[1];
}


die;



function get_page($url , $timeout = 10) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

    $page = curl_exec($ch);

    curl_close($ch);

    return $page;
}

# end of script
